<template>
  <div class="cart">
    <Header/>
    <div class="cart-info">
        <h3 class="cart-top">购物车结算 <span>共1门课程</span></h3>
        <div class="cart-title">
           <el-row>
             <el-col :span="2">&nbsp;</el-col>
             <el-col :span="10">课程</el-col>
             <el-col :span="8">有效期</el-col>
             <el-col :span="4">价格</el-col>
           </el-row>
        </div>
        <div class="cart-item" v-for="course in course_list" :key="course.id">
          <el-row>
             <el-col :span="2" class="checkbox">&nbsp;&nbsp;</el-col>
             <el-col :span="10" class="course-info">
               <img :src="course.course_img" alt="">
                <span>{{ course.name }}</span>
             </el-col>
             <el-col :span="8"><span>永久有效</span></el-col>
             <el-col :span="4" class="course-price">¥{{ course.price }}</el-col>
           </el-row>
        </div>
<!--        <div class="cart-item">-->
<!--          <el-row>-->
<!--             <el-col :span="2" class="checkbox">&nbsp;&nbsp;</el-col>-->
<!--             <el-col :span="10" class="course-info">-->
<!--               <img src="/static/image/course_demo.png" alt="">-->
<!--                <span>python入门</span>-->
<!--             </el-col>-->
<!--             <el-col :span="8"><span>永久有效</span></el-col>-->
<!--             <el-col :span="4" class="course-price">¥99.50</el-col>-->
<!--           </el-row>-->
<!--        </div>-->
        <div class="calc">
            <el-row class="pay-row">
              <el-col :span="4" class="pay-col"><span class="pay-text">支付方式：</span></el-col>
              <el-col :span="8">
                <span class="alipay"><img src="/static/image/alipay2.png" alt=""></span>
                <span class="alipay wechat"><img src="/static/image/wechat.png" alt=""></span>
              </el-col>
              <el-col :span="8" class="count">实付款： <span>¥{{ total_price }}</span></el-col>
              <el-col :span="4" class="cart-pay"><span @click="gotopay">支付宝支付</span></el-col>
            </el-row>
        </div>
    </div>
    <Footer/>
  </div>
</template>

<script>
  import Header from "./common/Header"
  import Footer from "./common/Footer"

  export default {
    name:"Order",
    data(){
      return {
        id: localStorage.id || sessionStorage.id,
        order_id: sessionStorage.order_id || null,
        course_list: [],
        total_price: 0,
      }
    },
    components:{
      Header,
      Footer,
    },
    created(){

        let token = '';
        if (localStorage.token) {
          token = localStorage.token;
        }
        if (sessionStorage.token) {
          token = sessionStorage.token;
        }

        if (!token) {
          this.$confirm("对不起，你还没有登录")
          .then(() => {
            this.$router.push({
              name: 'login'
            })
          })
        }

       // 获取购物车的勾选商品信息
        this.$axios.get(`${this.$settings.Host}/cart/buy/`,{
          headers:{
            "Authorization":"jwt " + token,
          }
        }).then(response=>{
          this.course_list = response.data.course_list;
          this.total_price = response.data.total_price;
        }).catch(error=>{
          console.log(error.response);
        })

    },
    methods: {
      gotopay(){
        let token = '';
        if (localStorage.token) {
          token = localStorage.token;
        }
        if (sessionStorage.token) {
          token = sessionStorage.token;
        }
        if (!token) {
          this.$confirm("对不起，你还没有登录")
          .then(() => {
            this.$router.push({
              name: 'login'
            })
          })
        }

        // 提交结算, 生成订单
        // {
        //   pay_type: 1,
        //   use_credit: this.use_credit,
        //   credit: 0,
        //   use_coupon: this.use_coupon,
        //   coupon: this.coupon
        // }
        this.$axios.post(this.$settings.Host+"/orders/", {}, {
            headers:{
              // 注意:jwt后面必须有且只有一个空格!!!!
              "Authorization":"jwt " + this.token
            }
        }).then(response=>{
          console.log(response.data)
          // let _this = this;
          // this.$alert(response.data.message,"提示",{
          //   callback(){
          //     _this.$router.push(`orders/${response.data.order}`);
          //   }
          // })
        }).catch(error=>{
          console.log(error.response)
        })
      }
    }
  }
</script>

<style scoped>
.cart{
  margin-top: 80px;
}
.cart-info{
  overflow: hidden;
  width: 1200px;
  margin: auto;
}
.cart-top{
  font-size: 18px;
  color: #666;
  margin: 25px 0;
  font-weight: normal;
}
.cart-top span{
    font-size: 12px;
    color: #d0d0d0;
    display: inline-block;
}
.cart-title{
    background: #F7F7F7;
    height: 70px;
}
.calc{
  margin-top: 25px;
  margin-bottom: 40px;
}

.calc .count{
  text-align: right;
  margin-right: 10px;
  vertical-align: middle;
}
.calc .count span{
    font-size: 36px;
    color: #333;
}
.calc .cart-pay{
    margin-top: 5px;
    width: 110px;
    height: 38px;
    outline: none;
    border: none;
    color: #fff;
    line-height: 38px;
    background: #ffc210;
    border-radius: 4px;
    font-size: 16px;
    text-align: center;
    cursor: pointer;
}
.cart-item{
  height: 120px;
  line-height: 120px;
  margin-bottom: 30px;
}
.course-info img{
    width: 175px;
    height: 115px;
    margin-right: 35px;
    vertical-align: middle;
}
.alipay{
  display: inline-block;
  height: 48px;
}
.alipay img{
  height: 100%;
  width:auto;
}

.pay-text{
  display: block;
  text-align: right;
  height: 100%;
  line-height: 100%;
  vertical-align: middle;
  margin-top: 20px;
}
</style>
